package com.csu.client.module.vo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.daohang361.imqq.ui.EIMTalkFrame;
/**
 * 
 * @author 胡海亮  QQ:249782944
 * 学校:中南大学 商学院
 * EnterpriseIMClient :企业即时通讯
 * May 12, 2010
 */
public class Cache {

	private static Cache instance;
	
	private List<MsgFriendGroup> friendGroups;
	private List<MsgTeam> teams;
	private boolean isChange=false;
	private Map<String ,MsgUser> friendMap=new HashMap<String ,MsgUser>();
	private Map<String,EIMTalkFrame> talkFrameMap=new HashMap<String ,EIMTalkFrame>();
	
	public static void setInstance(Cache instance) {
		Cache.instance = instance;
	}

	public static Cache getInstance() {
		if(instance==null){
			instance=new Cache();
		}
		return instance;
	}
	
	private Cache(){
		 friendGroups=new ArrayList<MsgFriendGroup>();
		 teams=new ArrayList<MsgTeam>();
	}
	
	public Map<String ,MsgUser> getfriendMap(){
		if(!isChange){
			isChange=true;
			for(MsgFriendGroup group:friendGroups){
				for(MsgUser user:group.getUserList()){
					friendMap.put(user.getUserID(), user);
				}
			}
		}
		return friendMap;
	}
	
	public EIMTalkFrame getTalkFrame(String userId){
		EIMTalkFrame frame=talkFrameMap.get(userId);
		if(null==frame){
			MsgUser user=this.getUserByID(userId);
			frame=new EIMTalkFrame(user);
			this.addTalkFrame(userId, frame);
		}
		return frame;
	}
	
	public void addTalkFrame(String userId,EIMTalkFrame frame){
		this.talkFrameMap.put(userId, frame);
	}
	
	public void removeTalkFrame(String userId){
		this.talkFrameMap.remove(userId);
	}
	
	public MsgUser getUserByID(String id){
		return this.getfriendMap().get(id);
	}
	
	public List<MsgFriendGroup> getFriendGroups() {
		return friendGroups;
	}

	public void setFriendGroups(List<MsgFriendGroup> friendGroups) {
		this.friendGroups = friendGroups;
	}

	public List<MsgTeam> getTeams() {
		return teams;
	}

	public void setTeams(List<MsgTeam> teams) {
		this.teams = teams;
	}


	public Map<String, EIMTalkFrame> getTalkFrameMap() {
		return talkFrameMap;
	}

	public void setTalkFrameMap(Map<String, EIMTalkFrame> talkFrameMap) {
		this.talkFrameMap = talkFrameMap;
	}
}
